package com.kkd.kkdapp.adapter;

import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.ExperienceParticularsActivity;
import com.kkd.kkdapp.activity.RegularParticularsActivity;
import com.kkd.kkdapp.http.entity.HomeTender;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Tools;
import com.kkd.kkdapp.view.RiseProgressBar;
import com.kkd.kkdapp.view.shapeimageview.PorterShapeImageView;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class InTheBiddingAdapter extends BaseAdapter {

	private Context mContext;
	private List<HomeTender> mList;
	public boolean mIsFirstAnimation = true;

	public InTheBiddingAdapter(Context mContext, List<HomeTender> mList) {
		this.mContext = mContext;
		this.mList = mList;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		return getBidView(position, convertView, parent);
	}

	public View getBidView(final int position, View convertView,
			ViewGroup parent) {

		ViewHolder mViewHolder;

		if (convertView == null
				|| !(convertView.getTag() instanceof ViewHolder)) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.inthebidding_list_item, null);
			mViewHolder = new ViewHolder();
			// 上部点击进入标的详情界面
			mViewHolder.top = (RelativeLayout) convertView
					.findViewById(R.id.top);
			// 标题
			mViewHolder.refund_name = (TextView) convertView
					.findViewById(R.id.refund_name);
			// 标的类型
			mViewHolder.type_bg = (PorterShapeImageView) convertView
					.findViewById(R.id.type_bg);
			mViewHolder.type_tv = (TextView) convertView
					.findViewById(R.id.type_tv);
			mViewHolder.bid_type_img = (ImageView) convertView
					.findViewById(R.id.bid_type_img);
			// 年利率
			mViewHolder.interest_tv = (TextView) convertView
					.findViewById(R.id.interest_tv);
			// 加息
			mViewHolder.rise_tv1 = (TextView) convertView
					.findViewById(R.id.rise_tv1);
			mViewHolder.zhanqi_count = (TextView) convertView
					.findViewById(R.id.zhanqi_count);
			// 理财期限
			mViewHolder.deadline_tv = (TextView) convertView
					.findViewById(R.id.deadline_tv);
			// 理财期限单位
			mViewHolder.deadline_company_tv = (TextView) convertView
					.findViewById(R.id.deadline_company_tv);
			// 按钮
			mViewHolder.bid = (TextView) convertView.findViewById(R.id.bid);
		
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		/*************** 标题 *****************/
		mViewHolder.refund_name.setTag(position);
		mViewHolder.refund_name.setText(mList.get(position).getBidTitle());
		/*************** 标的类型 **************/
		mViewHolder.type_bg.setTag(position);
		mViewHolder.type_tv.setTag(position);
		mViewHolder.bid_type_img.setBackgroundColor(Color.parseColor(mList.get(
				position).getBidTypeColor()));
		if (TextUtils.isEmpty(mList.get(position).getBidTypeName3())) {
			mViewHolder.type_tv.setVisibility(View.GONE);
			mViewHolder.type_bg.setVisibility(View.GONE);
		} else {
			mViewHolder.type_tv.setVisibility(View.VISIBLE);
			mViewHolder.type_bg.setVisibility(View.VISIBLE);
			mViewHolder.type_tv.setText(mList.get(position).getBidTypeName3());
			mViewHolder.type_tv.setTextColor(Color.parseColor(mList.get(
					position).getBidTypeColor3()));
			mViewHolder.type_bg.setImageColor(Color.parseColor(mList.get(
					position).getBidTypeColor3()));
			LayoutParams params = mViewHolder.type_bg.getLayoutParams();
			params.width = SystemUtils.dip2px(mContext, 11 * mList.get(position)
					.getBidTypeName3().length() + 12);
			mViewHolder.type_bg.setLayoutParams(params);
		}

		/**************** 年利率 **************/
		mViewHolder.interest_tv.setTag(position);
		mViewHolder.rise_tv1.setTag(position);
		String interestDisplay = mList.get(position).getInterestDisplay();
		if (interestDisplay.indexOf("+") == -1) {
			mViewHolder.interest_tv.setText(Tools.intOrDouble(Double.parseDouble(interestDisplay)));
			mViewHolder.rise_tv1.setText("");
		} else {
			String[] idp = interestDisplay.split("\\+");
			mViewHolder.interest_tv.setText(Tools.intOrDouble(Double.parseDouble(idp[0])));
			mViewHolder.rise_tv1.setText("+" + Tools.intOrDouble(Double.parseDouble(idp[1])) + "%");
		}
		/*************** 标的期限 *****************/
		mViewHolder.deadline_tv.setTag(position);
		mViewHolder.deadline_tv.setText(mList.get(position).getBidPeriod()
				+ "");
		/*************** 标的期限单位 ******************/
		String bidPeriodType = mList.get(position).getBidPeriodType();
		if ("月".equals(bidPeriodType)) {
			mViewHolder.deadline_company_tv.setText("个" + bidPeriodType);
		} else if ("天".equals(bidPeriodType)) {
			mViewHolder.deadline_company_tv.setText(bidPeriodType);
		}
		
		String bidPeriodWeb = mList.get(position).getBidPeriodWeb();
		int p = bidPeriodWeb.indexOf("+");
		if(p>0){
			mViewHolder.zhanqi_count.setVisibility(View.VISIBLE);
			String bidPeriodType2= bidPeriodWeb.substring(p);
			mViewHolder.zhanqi_count.setText(bidPeriodType2);
		}else{
			mViewHolder.zhanqi_count.setVisibility(View.GONE);
		}
		/***************** 标的状态 *******************/
		mViewHolder.bid.setTag(position);
		String bidStatus = mList.get(position).getBidStatus();
		if ("5001".equals(bidStatus)) {
			mViewHolder.bid
					.setBackgroundResource(R.drawable.tender_bt_selector);
			mViewHolder.bid.setEnabled(false);
		} else if ("5002".equals(bidStatus)) {
			mViewHolder.bid
					.setBackgroundResource(R.drawable.tender_bt_selector);
			mViewHolder.bid.setEnabled(true);
		} else if ("5003".equals(bidStatus)) {
			mViewHolder.bid.setBackgroundResource(R.drawable.tender_img_or);
			// mViewHolder.bid.setEnabled(false);
		} else if ("5004".equals(bidStatus)) {
			mViewHolder.bid.setBackgroundResource(R.drawable.tender_img_returning);
			// mViewHolder.bid.setEnabled(false);
		} else if ("5005".equals(bidStatus)) {
			mViewHolder.bid
					.setBackgroundResource(R.drawable.tender_bt_selector);
			mViewHolder.bid.setEnabled(false);
		} else if ("5006".equals(bidStatus)) {
			mViewHolder.bid
					.setBackgroundResource(R.drawable.tender_bt_selector);
			mViewHolder.bid.setEnabled(false);
		} else if ("5007".equals(bidStatus)) {
			 mViewHolder.bid
			 .setBackgroundResource(R.drawable.tender_bt_selector);
			mViewHolder.bid.setEnabled(false);
		} else if ("4011".equals(bidStatus)) {
			mViewHolder.bid
					.setBackgroundResource(R.drawable.tender_bt_selector);
			mViewHolder.bid.setEnabled(false);
		}

		mViewHolder.top.setOnClickListener(new OnClickListener() {
			String bidId = mList.get(position).getBidId();
			String bidType = mList.get(position).getBidType();
			String appbiddetail = mList.get(position).getAppbiddetail();

			@Override
			public void onClick(View v) {
				SystemUtils.onEvent("Past_Detail", 1);
				if ("501".equals(bidType)) {
					Intent intent = new Intent(mContext,
							ExperienceParticularsActivity.class);
					intent.putExtra("bidId", bidId);
					intent.putExtra("appbiddetail", appbiddetail);
					mContext.startActivity(intent);
				} else {
					Intent intent = new Intent(mContext,
							RegularParticularsActivity.class);
					intent.putExtra("bidId", bidId);
					intent.putExtra("appbiddetail", appbiddetail);
					mContext.startActivity(intent);
				}
			}
		});
		return convertView;
	}

	class ViewHolder {
		RelativeLayout top;
		TextView refund_name;
		TextView interest_tv;
		TextView deadline_tv;
		TextView bid;
		TextView zhanqi_count;
		PorterShapeImageView type_bg;
		TextView type_tv, deadline_company_tv;
		ImageView bid_type_img;
		TextView rise_tv1;

	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			mIsFirstAnimation = false;
		}
	};

	/**
	 * notifyKkdDataSetChanged:(这里用一句话描述这个方法的作用). <br/>
	 * 
	 * @author askzhao
	 */
	public void notifyKkdDataSetChanged() {
		notifyDataSetChanged();
		if (mIsFirstAnimation) {
			handler.sendEmptyMessageDelayed(1, 1000);
		}
	}

}
